package com.xxx.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.xxx.base.vo.ArticleTypeVo;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.mapper.ArticleTypeMapper;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ArticleTypeImpl implements IArticleTypeService {
    @Autowired
    ArticleTypeMapper articleTypeMapper;

    @Override
    public List<ArticleType> findAll() {
        return articleTypeMapper.findAll();
    }

    @Override
    public ArticleType findOne(long id) {
        return articleTypeMapper.findOne(id);
    }

    @Override
    public List<ArticleType> queryPage(ArticleTypeQuery articleTypeQuery) {
        PageHelper.startPage(articleTypeQuery.getCurrentPage(), articleTypeQuery.getPageSize());
        List<ArticleType> articleTypes = articleTypeMapper.queryPage(articleTypeQuery);
        return articleTypes;
    }

    @Override
    public void addArticleType(ArticleType articleType) {

        articleTypeMapper.addArticleType(articleType);
    }

    @Override
    public void updateArticleType(ArticleType articleType) {
        articleTypeMapper.updateArticleType(articleType);
    }

    @Override
    public void deleteArticleType(long id) {
        articleTypeMapper.deleteArticleType(id);
    }

    @Override
    public void batchArticleType(Long[] ids) {
        articleTypeMapper.batchArticleType(ids);
    }

    @Override
    public  List<ArticleType> getTypeTree() {
        List<ArticleType>articleTypesTree=new ArrayList<>();
        List<ArticleType> all = articleTypeMapper.findAll();
        Map<Long,ArticleType>map=new HashMap<>();
        for (ArticleType articleType : all) {
            map.put(articleType.getId(),articleType);
        }
        for (ArticleType articleType : all) {
            Long parentId = articleType.getParentId();
            if(parentId==null){
                articleTypesTree.add(articleType);
            }else {
                ArticleType articleType1 = map.get(parentId);
                articleType1.getChildren().add(articleType);
            }
        }
        return articleTypesTree;
    }

    @Override
    public List<ArticleTypeVo> getArticleTypeStatistics() {
        return articleTypeMapper.getArticleTypeStatistics();
    }
}
